package cn.edu.stu.syllabus.repository.oaFile;

import java.util.ArrayList;
import java.util.List;

import cn.edu.stu.syllabus.repository.oaFile.local.OaFileEntry;
import cn.edu.stu.syllabus.repository.oaFile.remote.OaFile;
import cn.edu.stu.syllabus.utils.ListUtils;

/**
 * Created by daidaijie on 2017/3/23.
 */

public class OaFileConverter {

    public static List<OaFileEntry> convert(List<OaFile> oaFiles) {
        List<OaFileEntry> oaFileEntries = new ArrayList<>();
        if (ListUtils.getListSizeSafety(oaFiles) > 0) {
            for (OaFile oaFile : oaFiles) {
                oaFileEntries.add(convert(oaFile));
            }
        }
            return oaFileEntries;
    }

    public static OaFileEntry convert(OaFile oaFile) {
        OaFileEntry oaFileEntry = new OaFileEntry();
        String fileName = oaFile.getIMAGEFILENAME();
        int lastDotIndex = fileName.lastIndexOf(".");
        if (lastDotIndex != -1) {
            oaFileEntry.setFileType(
                    fileName.substring(lastDotIndex + 1, fileName.length()).toUpperCase());
        } else {
            oaFileEntry.setFileType("FILE");
        }
        oaFileEntry.setFileName(fileName);
        oaFileEntry.setFileLink("http://notes.stu.edu.cn/weaver/weaver.file.FileDownload?fileid="
                + oaFile.getIMAGEFILEID() + "&download=1&requestid=0");
        return oaFileEntry;
    }
}
